package com.tensai.teach.beijing.utils.files;

import com.tensai.teach.beijing.model.old.base.MapData;
import com.tensai.teach.beijing.model.old.base.second.Features;
import com.tensai.teach.beijing.model.old.base.second.features.geometry.OtherGeo;
import com.tensai.teach.beijing.model.old.base.second.features.properties.RoadConProperties;
import com.tensai.teach.beijing.utils.EntityServiceUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.*;

/**
 * @author Tensai
 */
@Slf4j
public class RoadConService {

	public static Map<Long, Long> toFrom = new HashMap<>();
	public static Map<Long, Long> fromTo = new HashMap<>();

	//TODO
	public List<Long> getRoadIdListBetween(Long from, Long to) {
		log.info("from - {}, to - {}", from, to);
		if (Objects.isNull(from) || Objects.isNull(to)) {
			log.error("error1");
			return null;
		}
		List<Long> list = new ArrayList<>();
		list.add(from);
		if (!from.equals(to)) {
			MapData<RoadConProperties, OtherGeo> roadCon = EntityServiceUtil.getRoadCon();
			if (toFrom.size() == 0 || fromTo.size() == 0) {
				roadCon.getFeatures().stream()
						.map(Features::getProperties)
						.forEach(it -> {
							toFrom.put(it.getToRoad(), it.getFromRoad());
							fromTo.put(it.getFromRoad(), it.getToRoad());
						});
			}
			Long tmp = from;
			boolean flag = true;
			while (flag) {
				Long tmp1 = fromTo.get(tmp);
				tmp = tmp1;
				if (Objects.nonNull(tmp1) && !tmp1.equals(to)) {
					list.add(tmp1);
					log.info(tmp1.toString());
				} else {
					flag = false;
				}
			}
			list.add(to);
		}
		return list;
	}

	@Test
	public void test() {
		log.info(getRoadIdListBetween(1871179L, 1871151L).toString());
	}
}
